/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package unb.wikinarua.geopartitura;

import java.util.ArrayList;
import java.util.List;

import unb.wikinarua.geopartitura.service.xmpp.XMPPConnectionService;
import unb.wikinarua.geopartitura.util.Constants;
import unb.wikinarua.geopartitura.view.MenuButtonView;
import unb.wikinarua.geopartitura.view.MenuLineView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;

public class Menu extends Activity {
	
	private static final int START = 0;
	private static final int SETTINGS = 1;
	private static final int CREDITS = 2;
	
	private List<MenuButtonView> options = new ArrayList<MenuButtonView>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.menu);
		LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		
		// Settings
		updatePreferences();
		
		// Start XMPP Service
		startService(new Intent(this, XMPPConnectionService.class));
		
		// Android ID
		TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
		if (tm.getDeviceId() != null)
			Constants.ANDROID_ID = tm.getDeviceId();
		
		// Create Menu Buttons
		options.add(new MenuButtonView(this, START, R.drawable.start, -20, -80));
		options.add(new MenuButtonView(this, SETTINGS, R.drawable.settings, -60, 80));
		options.add(new MenuButtonView(this, CREDITS, R.drawable.credits, 90, 40));
		
		addContentView(new MenuLineView(this, options), layout);

		// Add Menu Buttons
		for (int i = 0; i < options.size(); i++) {
			addContentView(options.get(i), layout);
		}
	}
	
	@Override
	protected void onResume() {
		updatePreferences();
		super.onResume();
	}
	
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	stopService(new Intent(this, XMPPConnectionService.class));
    }
	
	public Handler handler = new Handler() {
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case START:
				Log.i(Constants.TAG, "Start");
				startActivity(new Intent(Menu.this, Main.class));
				break;
			case SETTINGS:
				Log.i(Constants.TAG, "Settings");
				startActivity(new Intent(Menu.this, Preferences.class));
				break;
			case CREDITS:
				Log.i(Constants.TAG, "Credits");
				startActivity(new Intent(Menu.this, Credits.class));
				break;
			}
			overridePendingTransition(R.anim.fade, R.anim.hold);
			super.handleMessage(msg);
		}
		
	};

	public void updatePreferences() {
		SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
//		Constants.UPDATE_TIME = Long.valueOf(pref.getString("update", "15000"));
	}
	
}
